/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Manager.Update;

import Entity.Barang;
import Manager.TestConnection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author evan_utomo
 */
public class UpdateController {
     public List<Barang> getAll() throws SQLException, UpdateException {
        Manager.TestConnection connec = new TestConnection();
        Statement st = connec.getConnection().createStatement();
        ResultSet rs = st.executeQuery("select * from BARANG order by KODEBARANG");
        List<Barang> list = new ArrayList<Barang>();
        while (rs.next()) {
            Barang o = new Barang();
            o.setKodebarang(rs.getString("KODEBARANG"));
            o.setNamabarang(rs.getString("NAMABARANG"));
            o.setJenisbarang(rs.getString("JENISBARANG"));
            o.setStock(rs.getInt("STOK"));
            o.setHargabarang(rs.getDouble("HARGABARANG"));
            list.add(o);
        }
        return list;
    }

    public void insert(Barang o) throws SQLException {
        Manager.TestConnection connec = new TestConnection();
        PreparedStatement ps = connec.getConnection().prepareStatement("insert into BARANG values(?,?,?,?,?)");
        ps.setString(1, o.getKodebarang());
        ps.setString(2, o.getNamabarang());
        ps.setString(3, o.getJenisbarang());
        ps.setInt(4, o.getStock());
        ps.setDouble(5, o.getHargabarang());
        ps.executeUpdate();
////        return o;
       
    }
    public void update(Barang o) throws SQLException {
        Manager.TestConnection connec = new TestConnection();
        PreparedStatement ps = connec.getConnection().prepareStatement("update BARANG set NAMABARANG=?,"
                + "JENISBARANG=?, STOK=?,"
                + "HARGABARANG=? where KODEBARANG=?");
        
        ps.setString(1, o.getNamabarang());
        ps.setString(2, o.getJenisbarang());
        ps.setInt(3, o.getStock());
        ps.setDouble(4, o.getHargabarang());
        ps.setString(5, o.getKodebarang());
        ps.executeUpdate();
    }
     
    public void delete(String o) throws SQLException {
        Manager.TestConnection connec = new TestConnection();
        PreparedStatement ps = connec.getConnection().prepareStatement("delete from BARANG where KODEBARANG=?");
        ps.setString(1, o);
        ps.executeUpdate();
    }

}
